package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.common.BaseController;
import com.inspur.his.constant.Constant;
import com.inspur.his.pojo.Drugs;
import com.inspur.his.service.DrugsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;




@Controller
public class DrugsController extends BaseController {
    @Autowired
    DrugsService drugsService;

    @RequestMapping("drugsListController")
    public String drugsList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn){
        //获取页面提交来的查询条件
        String ypname = request.getParameter("ypname");
        String drugsid = request.getParameter("drugsid");
        //  点击用户管理左侧菜单进行默认无条件查询
        HashMap map = new HashMap();
        map.put("ypname",ypname);
        map.put("drugsid",drugsid);
        PageHelper.startPage(pn,5);
        List<Drugs> drugsList = drugsService.drugsList(map);
        PageInfo<Drugs> pageInfo = new PageInfo<Drugs>(drugsList,drugsList.size());
        model.addAttribute("drugsList",drugsList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("ypname",ypname);
        model.addAttribute("ypid",drugsid);
        return "drugs/drugsList.jsp";
    }
    @RequestMapping("drugsAddController")
    public String drugsAdd(Drugs drugs,Model model) throws Exception {

        Date day = new Date();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-DD");
        String today = fm.format(day);
        drugs.setIntroducedate(fm.parse(today));
        drugs.setProductdate(fm.parse(today));
        drugs.setProductdate(fm.parse(today));
        drugsService.drugsAdd(drugs);
        model.addAttribute("drugs",drugs);
        model.addAttribute("flag",Constant.USER_STATUS_ABLE);
        model.addAttribute("success","药品信息保存成功！");
        return "drugs/drugsAdd.jsp";
    }


    @RequestMapping("/drugsPriceController")
    public  String drugspricecheck(String id,Model model) {
        Drugs drugs = drugsService.getDrugsById(id);
        model.addAttribute("drugs",drugs);
        return "/drugs/drugsModify.jsp";
    }

    @RequestMapping("/drugsPriceUpdateController")
    public  String drugspriceupdate(Drugs drugs,Model model) {
        drugsService.updatePriceDrugs(drugs);
        model.addAttribute("success","药品价格信息修改成功");
        model.addAttribute("drugs",drugs);
        model.addAttribute("flag","1");
        return "redirect:drugsListController";
    }


    @RequestMapping("/drugsnumController")
    public  String drugsnumucheck(String id,Model model) {
        Drugs drugs = drugsService.getDrugsById(id);
        model.addAttribute("drugs",drugs);
        return "drugs/ypcz.jsp";
    }

    @RequestMapping("/drugsnumupdateController")
    public  String drugsnumupdate(Drugs drugs,Model model) {
        drugsService.updateNumDrugs(drugs);
        model.addAttribute("success","药品数量信息修改成功");
        model.addAttribute("drugs",drugs);
        model.addAttribute("flag","1");
        return "redirect:drugsListController";
    }

    @RequestMapping("/deleteDrugsController")
    public  String deleteDrugs(String id,Model model) {
        drugsService.deleteDrugsById(id);
        return "redirect:drugsListController";
    }
}